package com.bafa.services;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.*;

import com.bafa.dbcache.*;
import com.bafa.dbcache.GrabSeasons;
import com.bafa.dbcache.GrabTeams;
import com.bafa.debug.Debug;
import com.bafa.exceptions.TeamDoesNotExistException;
import com.bafa.jdbc.DBAccess;
import com.bafa.jdbc.QueryObject;
import com.bafa.middle.betwixt.Utils;
import com.bafa.objects.Email;
import com.bafa.objects.Match;
import com.bafa.objects.PageBean;
import com.bafa.objects.season.Division;
import com.bafa.objects.season.Season;
import com.bafa.objects.team.Team;
import com.bafa.objects.team.TeamSeasonStats;
import com.bafa.objects.player.Player;

public class RetrieveServices implements Serializable {

	public static Team[] getAllTeams() {
		return (Team[]) GrabTeams.getTeams().values().toArray(
				new Team[GrabTeams.getTeams().size()]);

	}

	public static Integer[] getAllUnplayedMatches() {
		return (Integer[]) LeagueCache.getUnplayedMatches().keySet().toArray(
				new Integer[LeagueCache.getUnplayedMatches().size()]);

	}

	public static Season getCurrentSeason() {

		Season season = getSeason(null);
		return season;
	}

	public static Division getDivision(String seasonId, String divisionId) {

		Season season = getSeason(seasonId);

		if (season == null)
			return null;
		if (divisionId == null && season.getDivisions().size() > 0) {
			divisionId = ((Integer) season.getDivisions().firstKey())
					.toString();
		}

		if (season.getDivisions().size() < 1) {
			return new Division();
		}
		Division division = (Division) season.getDivisions().get(new Integer(divisionId));

		return division;

	}

	public static String[] getDivisionTopScorers(int divisionId, String seasonId) {
		Season season = getSeason(seasonId);
		Division division = (Division) season.getDivisions().get(divisionId);

		return division.topScorers();
	}

	public static Season[] getLeagueSeasons() {

		return GrabSeasons.getLeagueSeasons();
	}

	public static Match getMatch(int matchId) {
		return (Match) LeagueCache.getMatches().get(matchId);
	}

	public static Player getPlayer(String firstname) {
		return (Player) LeagueCache.getPlayers().get(
				firstname.toUpperCase());
	}

	public static Season getSeason(String seasonId) {
		Season season = null;
		try {
			if (seasonId == null)
				season = GrabSeasons.getCurrentSeason();
			else
				season = GrabSeasons.getSeason(new Integer(seasonId));

		} catch (Exception e) {
			e.printStackTrace();
		}
		return season;
	}

	public static Team getTeam(String teamId) {
		Team team = (Team) GrabTeams.getTeams().get(new Integer(teamId));

		return team;

	}

	public static Team getTeamByName(String teamName)
			throws TeamDoesNotExistException {
		Team team = GrabTeams.getTeamsByName().get(teamName.toUpperCase());

		if(team==null){
			throw new TeamDoesNotExistException(teamName);
		}
		
		return team;

	}

	public static PageBean getPage(String id){
		
		try{
			PreparedStatement query = new DBAccess().conn.prepareStatement("select * from pages where id=?");
	        query.setString(1, id);
			ResultSet rs = query.executeQuery();
			while(rs.next()){
				PageBean pageBean = new PageBean(rs.getString("id"),rs.getString("LHS"),
												 rs.getString("mainbody"),
												 rs.getString("mainheader"),
												 rs.getString("pageheader"),
												 rs.getString("event"),
												 (rs.getInt("user")==1),
												 (rs.getInt("admin")==1), (rs.getInt("sendemail")==1), rs.getString("email"));
				return pageBean;
			}
		}catch(Exception e){
			Debug.printStackTrace(e);
		}
		return null;
	}
	
	public static Email getEmail(String id){
		QueryObject query = new QueryObject("emails");
		query.addWhereAndField("id",id);
		try{
			ResultSet rs = new DBAccess().query(query);
			while(rs.next()){
				
				Email email = (Email)Utils.readBetwixt(new String(rs.getBytes("email")),"email",Email.class);
				return email;
			}
		}catch(Exception e){
			Debug.printStackTrace(e);
		}
		return null;
	}
	
	public static String getPhotoId(String id){
		try{
			PreparedStatement query = new DBAccess().conn.prepareStatement("select * from photos where id=?");
	        query.setString(1, id);
			ResultSet rs = query.executeQuery();
			while(rs.next()){
				return rs.getString("photoid");
			}
		}catch(Exception e){
			Debug.printStackTrace(e);
		}
		return null;
	}
	public static String getPhotoVersion(String id){
		try{
			PreparedStatement query = new DBAccess().conn.prepareStatement("select * from photos where id=?");
	        query.setString(1, id);
			ResultSet rs = query.executeQuery();
			while(rs.next()){
				return rs.getString("version");
			}
		}catch(Exception e){
			Debug.printStackTrace(e);
		}
		return null;
	}
	public static void main(String[] args) {
		LeagueCache.getAll();

		System.out.println(RetrieveServices.getDivision("36", null));
	}
}
